﻿using System.Data.Entity;

using System.Collections.Generic;
using BBICMS.Repository;
using BBICMS.Model;
using System.Linq;
using System.Data.Entity.Infrastructure;

namespace BBICMS.Repository
{
    public class PostsRepository : GenericRepository<Post>, IPostRepository
    {
        public PostsRepository(DbContext context) : base(context) { }

        public IEnumerable<Post> GetPageArticles(int PageIndex, int PageSize, int? BlogId, string Keyword, out int Count)
        {
            var query = this._dbSet.OrderByDescending(p=>p.Pubdate).Include(p => p.Blog).AsQueryable();


            if (BlogId > 0)
            {
                query = query.Where(p => p.BlogId == BlogId);
            }

            if (!string.IsNullOrEmpty(Keyword))
            {
                query = query.Where(p => p.Title.Contains(Keyword));
            }

            Count = query.Count();

            return query.Skip(PageIndex * PageSize).Take(PageSize).ToList();
        }
    }
}
